//
//  ScoringRulesVC.m
//  Cunli
//
//  Created by lilubin on 15/8/13.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ScoringRulesVC.h"
#import "ScoringCell.h"


@interface ScoringRulesVC () <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_titlesArray;
    NSArray *_contentsArray;
    NSArray *_footerContents;
}

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation ScoringRulesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _titlesArray = @[@"登录", @"发布", @"阅读",@"学习", @"评论", @"分享", @"对话", @"资料", @"邀请"];
    _contentsArray = @[
                       @"每天登录“村里”APP均可获得1积分，连续登录七天可额外奖励5积分。",
                       @"单次发布信息，如邻里互助，乡里乡情等均可获得积分，仅发布文字获得2积分奖励，发布文字＋图片获得3积分。",
                       @"阅读法制资讯可获得1积分，包括“外面事”，“周围事”，“每日一谈”，“每日一法”。",
                       @"学习法制资源可获得5积分或更高积分，包括“法律法规”，“普法教材”，“普法讲堂”。",
                       @"单项评论他人发布的信息或回复他人的评论可获取2积分。",
                       @"单次将“村里”APP中的信息分享到第三方社交应用均可获得5积分。",
                       @"与调解员或律师进行视频，对话可获得1积分，服务者可获得5积分。",
                       @"完善个人资料信息，设置头像、姓名等均可获得10积分。此积分为一次性获得，修改无积分奖励。",
                       @"邀请亲人注册使用“村里”APP，每邀请一人且确定被邀请人登录“村里”APP可获得10积分。"
                       ];
    
    _footerContents = @[
                        @"1.积分总和为每次获得积分累加所得，积分兑换后将减去兑换物品所花的积分。",
                        @"2.“村里”鼓励，支持用户发布有助于社会团结，和谐的信息，如存在明显的散播谣言，发布违反社会稳定，和谐的信息，我们有权不予发放积分或收缴已发放的积分。",
                        @"3.村里所有商品由杭州湛亚科技有限公司合作商提供。",
                        @"4.村里积分规则最终解释权归杭州湛亚科技有限公司所有。",
                        @"5.客服电话：4000808135（工作日：8:30～17:30）。"
                        ];
    
    [self initTableView];
    [self registerCell];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)registerCell
{
    UINib *nib = [UINib nibWithNibName:@"ScoringCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"ScoringCell"];
}

- (void)initTableView
{
    CGRect newFrame = self.view.frame;
    CGFloat newHeight = newFrame.size.height-64;
    newFrame.size.height = newHeight;
    
    _tableView = [[UITableView alloc] initWithFrame:newFrame style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.bounds];
    [imageView setImage:[UIImage imageNamed:@"scoreBanner"]];
    [headerView addSubview:imageView];
    self.tableView.tableHeaderView = headerView;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    self.navigationItem.title = @"积分规则";
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < 9)
    {
        ScoringCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ScoringCell"];
        cell.titleLabel.text = _titlesArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.contentStr = _contentsArray[indexPath.row];
        return cell;
    }
    else if(indexPath.row == 9)
    {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.textLabel.text = @"其 他 规 则";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor redColor];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = _footerContents[indexPath.row-10];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.numberOfLines = 0;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 9) {
        UILabel *propertyLabel = [UILabel new];
        propertyLabel.font = [UIFont systemFontOfSize:13];
        propertyLabel.text = _footerContents[indexPath.row-10];
        propertyLabel.numberOfLines = 0;
        CGSize newlabelSize = [_footerContents[indexPath.row-10] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
        return newlabelSize.height;
    }
    return 90;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"如 何 赚 取 积 分 ?";
    label.textColor = [UIColor redColor];
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

@end
